package com.leon.notepad.content;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.TimePickerView;
import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.TieBean;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.leon.notepad.R;
import com.leon.notepad.content.adapter.MyNotepadRecyclerAdapter;
import com.leon.notepad.content.bean.NotepadBean;
import com.leon.notepad.content.utils.DateUtils;
import com.leon.notepad.content.utils.WrapContentLinearLayoutManager;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MonthActivity extends BaseActivity {

    @BindView(R.id.ll_month)
    LinearLayout llMonth;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_month)
    TextView tvMonth;

    private MyNotepadRecyclerAdapter adapter;
    private List<NotepadBean> mList = new ArrayList<>();
    private static final int REQUEST_SETTING_MONEY = 1011;

    private Date mDate=new Date();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month);
        ButterKnife.bind(this);

        //设置布局管理器
        recyclerview.setLayoutManager(new WrapContentLinearLayoutManager(this, 1));
        adapter = new MyNotepadRecyclerAdapter(mList, this);
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new MyNotepadRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(MonthActivity.this, DetailsNotepadActivity.class);
                intent.putExtra("item", mList.get(position));
                startActivityForResult(intent, REQUEST_SETTING_MONEY);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                List<TieBean> datass = new ArrayList<>();
                datass.add(new TieBean("修改"));
                datass.add(new TieBean("删除"));
                DialogUIUtils.showSheet(MonthActivity.this, datass,
                        "取消", Gravity.BOTTOM, true, true, new DialogUIItemListener() {
                            @Override
                            public void onItemClick(CharSequence text, int i) {
                                Log.e("1111", "!!!!!!!!" + position);
                                if (i == 1) {
                                    LitePal.delete(NotepadBean.class, mList.get(position).getId());
                                    mList.remove(position);
                                    adapter.notifyDataSetChanged();
                                } else if (i == 0) {
                                    Intent intent = new Intent(MonthActivity.this, AddNotepadActivity.class);
                                    intent.putExtra("item", mList.get(position));
                                    intent.putExtra("type", "modify");
                                    startActivityForResult(intent, REQUEST_SETTING_MONEY);
                                }
                            }
                        }).show();

            }
        });

    }

    @OnClick(R.id.ll_month)
    public void onClick() {
        //时间选择器
        TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                tvMonth.setText(DateUtils.getStrDate(date));
                mDate=date;
                mList.clear();
                mList.addAll(LitePal.where("time > ? and time <= ?",DateUtils.getFirstDayDateOfMonth(date).getTime()+"",DateUtils.getLastDayOfMonth(date).getTime()+"").find(NotepadBean.class));
                adapter.notifyDataSetChanged();
            }
        })
                .setType(new boolean[]{true, true, false, false, false, false}) //年月日时分秒 的显示与否，不设置则默认全部显示
                .isCenterLabel(false)
                .build();

        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SETTING_MONEY && resultCode == RESULT_OK) {
            mList.clear();
            mList.addAll(LitePal.where("time > ? and time <= ?",DateUtils.getFirstDayDateOfMonth(mDate).getTime()+"",DateUtils.getLastDayOfMonth(mDate).getTime()+"").find(NotepadBean.class));
            adapter.notifyDataSetChanged();
        }
    }

}